const app = getApp()
var flag=false;
Page({
  data:{
    addressId: undefined,
    
    addUrl: app.globalData.baseUrl +"/useraddressinfo/insertuseraddressinfo",
    modifyUrl: app.globalData.baseUrl +"/useraddressinfo/updateuseraddress"
  },
  //////地址选择器
  selectArea(e) {
    console.log(e.detail)
    this.setData({
      //address: e.detail,
      addressArea: e.detail[0].name + '-' + e.detail[1].name + '-' + e.detail[2].name       
    })

  }, 

  /////////
   onLoad: function(options) {
     var that = this;
     // 页面初始化 options为页面跳转所带来的参数
     this.setData({
       addressId: options.addressId
     });

     if (options.addressId == undefined) {
       let buyerOpenid = wx.getStorageSync("openid");
       console.log('openid:' + buyerOpenid);
       that.setData({
         buyerOpenid: buyerOpenid

       });
       return;
     }
     wx.request({
       url: app.globalData.baseUrl +"/useraddressinfo/getaddressinfobyid",
       data: { "addressId": options.addressId },
       method: 'GET',
       success: function (res) {
         var userAddressinfo = res.data.userAddressinfo;
         if (userAddressinfo == undefined) {
           var toastText = '获取数据失败' + res.data.errMsg;
           wx.showToast({
             title: toastText,
             icon: '',
             duration: 2000
           });
         } else {
           that.setData({
             addressUserName: userAddressinfo.addressUserName,
             addressBuyerPhone: userAddressinfo.addressBuyerPhone,
             addressArea: userAddressinfo.addressArea,
             addressDetail: userAddressinfo.addressDetail,
             buyerOpenid: userAddressinfo.buyerOpenid
           });
         }
       }
     })
   },
 

    areaPickerBindchange:function(e){
    this.setData({
      areaValue:e.detail.value
    })
  },
    addrePickerBindchange:function(e){
    this.setData({
      addreValue:e.detail.value
    })
  },
  
    //点击保存
  formSubmit: function(e) {
    var warn ="";
    var that = this;
    if (e.detail.value.addressUserName==""){
      warn = "请填写您的姓名！";
    } else if (e.detail.value.addressBuyerPhone==""){
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.addressBuyerPhone))){
      warn = "手机号格式不正确";
    } else if (e.detail.value.addressArea=='0'){
      warn = "请选择您的所在区域";
    } else if (e.detail.value.addressDetail==""){
      warn = "请输入您的具体地址";
    }else{
      flag=true;
      var formData = e.detail.value;
      console.log('formData:' + formData);
      var url = that.data.addUrl;
      if (that.data.addressId != undefined) {
        formData.addressId = that.data.addressId;
        url = that.data.modifyUrl;
      }
      wx.request({

        url: url,

        data: JSON.stringify(e.detail.value),
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          var result = res.data.success
          var toastText = "操作成功！";
          if (result != true) {
            toastText = "失败，检查信息" + res.data.errMsg;
          }
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
          //if (that.data.addressId == undefined) {
            /*wx.redirectTo({
              url: '../../address/chooseAddre/chooseAddre',
            })*/
            //返回上一级关闭当前页面
            wx.navigateBack({
              delta: 1
            })
          //}
        }
      })
    }
    if(flag==false){
      wx.showModal({
      title: '提示',
      content:warn
    })
    }
    
  },
  
  })